package main.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import main.graph.ValuedGraph;

import y.base.Edge;
import y.base.Node;

/**
 * represent the mapping betwee a form tree to an ontology. Two mappings:
 * 
 * node2onto: tree nodes to ontology elements
 * edge2onto: tree edges to ontology paths
 * 
 * treeNum: the tree number in the source file: formTrees.graph.mapping
 * 
 * @author YuanAn
 *
 */
public class Tree2OntoMapping {	
	
	private ValuedGraph graph;
	
	private HashMap<Node, OntoEle> node2onto;
	
	private HashMap<Edge, OntoPath> edge2onto;
	
	
	public Tree2OntoMapping(ValuedGraph g){
		
		graph = g;
		
		node2onto = new HashMap<Node, OntoEle>();
		
		edge2onto = new HashMap<Edge, OntoPath>();
	}
	
	
	public void addNode2Onto(Node n, OntoEle ele){
	
		node2onto.put(n, ele);
	
	}
	
	public void addEdge2Onto(Edge e, OntoPath p){
		
		edge2onto.put(e, p);
	}
	
	public HashMap<Edge, OntoPath> getEdge2Onto(){
		return edge2onto;
	}
	
	/*
	 * display the mapping in a string
	 */
	public String display(){
		
		String ans = "<treeNodes2onto mapping>\n";
		
		Set<Node> nodes = node2onto.keySet();
		
		Iterator<Node> nodeIt = nodes.iterator();
		while(nodeIt.hasNext()){
			Node node = nodeIt.next();
			OntoEle ele = node2onto.get(node);
			
			String nodeString = (String) graph.getNodeValue(node);
			String eleName = ele.getName();
			
			if(ele instanceof Attribute || ele instanceof Relationship)
				eleName = "<" + eleName + ">";
			else
				eleName = "[" + eleName + "]";
			
			ans += "[" + nodeString + "] :=: " + eleName + "\n";
		}
		ans += "</treeNodes2onto mapping>\n";
		
		ans += "<treeEdges2onto mapping>\n";
		
		Set<Edge> edges = edge2onto.keySet();
		
		Iterator<Edge> edgeIt = edges.iterator();
		while(edgeIt.hasNext()){
			Edge edge = edgeIt.next();
			OntoPath path = edge2onto.get(edge);
			
			Node source = edge.source();
			Node target = edge.target();
			
			ans += "[" + graph.getNodeValue(source) + "]" + "-" + "[" + graph.getNodeValue(target) + "]";
			ans += " :=: ";
			ans += path.display() + "\n";
			
		}
		ans += "</treeEdges2onto mapping>\n";
		
		return ans;
	}
	
}
